const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime
}

// 收藏工具：切换某首歌的收藏状态并返回最新列表
function toggleFav(song){
  const list = wx.getStorageSync('favs') || []
  const exist = list.findIndex(i=> i.id === song.id)
  if(exist>-1){ list.splice(exist,1) }
  else { list.unshift({ id: song.id, name: song.name, artist: song.artist, cover: song.cover }) }
  wx.setStorageSync('favs', list)
  return list
}

module.exports.toggleFav = toggleFav
